/*
 * @Author: wwssaabb
 * @Date: 2021-07-26 16:54:47
 * @LastEditTime: 2021-07-26 16:57:58
 * @FilePath: \react-admin-server\router\index.js
 */
//整合route目录的路由
const fs=require('fs')
const dir_arr=[]

function getRouters(path=__dirname){  //遍历route目录下的所有文件
  let f_arr=fs.readdirSync(path)
  f_arr.forEach(file=>{
    let states=fs.statSync(path+'\\'+file)
    if(states.isDirectory()){
      getRouters(path+'\\'+file)
    }else{
      dir_arr.push(__dirname+'\\'+file)
    }
  })
}

composeRouter=(app)=>{
  dir_arr.forEach(file=>{
    const router=require(file)
    app.use(router.routes()).use(router.allowedMethods())
  })
}

getRouters()
dir_arr.splice(dir_arr.findIndex(i=>i===__dirname+'\\index.js'),1)
//dir_arr.splice(dir_arr.findIndex(i=>i===__dirname+'\\user.js'),1)

module.exports=composeRouter